package config

import (
	"time"

	"github.com/fsnotify/fsnotify"
	"github.com/spf13/viper"
)

var (
	C = new(Config)
)
var (
	HostName = "your host name"
)

// Config .
type Config struct {
	PingApi     string        `yaml:"pingApi"`
	PingPolling time.Duration `yaml:"pingPolling" default:"1s"`
	Timeout     time.Duration `yaml:"timeout" default:"5s"`
}

// Init .
func Init(filePath string) (err error) {
	viper.SetConfigFile(filePath)
	err = viper.ReadInConfig()
	if err != nil {
		return err
	}
	err = viper.Unmarshal(C)
	if err != nil {
		return err
	}

	viper.WatchConfig()
	viper.OnConfigChange(func(e fsnotify.Event) {
		err = viper.Unmarshal(C)
		if err != nil {
			panic(err)
		}
	})
	return nil

}
